package com.resign.carl.composite.api.impl;

import com.google.common.collect.Lists;
import com.resign.carl.composite.api.MenuComponent;

import java.util.Iterator;
import java.util.List;

/**
 * 菜单类：持有菜单项或其它菜单
 * Created by carl.zhao on 2016/6/16.
 */
public class Menu extends MenuComponent {
    private List<MenuComponent> menuComponents = Lists.newArrayList();
    private String name;
    private String description;
    public Menu(String name, String description){
        this.name = name;
        this.description = description;
    }
    @Override
    public void add(MenuComponent menuComponent){
        if(null == menuComponent){
            return;
        }
        menuComponents.add(menuComponent);
    }
    @Override
    public void remove(MenuComponent menuComponent){
        if(null == menuComponent){
            return;
        }
        menuComponents.remove(menuComponent);
    }
    @Override
    public MenuComponent getChild(int i){
        if(i >= menuComponents.size()){
            throw new UnsupportedOperationException();
        }
        return menuComponents.get(i);
    }
    @Override
    public String getName(){
        return name;
    }
    @Override
    public String getDescription(){
        return description;
    }
    @Override
    public void print(){
        System.out.println("\n" + getName());
        System.out.println(", " + getDescription());
        System.out.println("---------------------");
        Iterator<MenuComponent> iterator = menuComponents.iterator();
        while(iterator.hasNext()){
            MenuComponent menuComponent = iterator.next();
            menuComponent.print();
        }
    }
    public Iterator createIterator(){
        return new CompositeIterator(menuComponents.iterator());
    }
}
